
#ifndef  _HTTPNODE_H_
#define  _HTTPNODE_H_


// #import <Foundation/Foundation.h>
#include <stdio.h>
#include "cocos2d.h"
#include "httpClient.h"
#include "Define.h"
#include "MyObj.h"

USING_NS_CC;
using namespace std;







// 单个下载任务数据记录.
struct OneTaskInfo
{
    int     taskId;
    int     imgNumber;
    std::string title;
    std::string url;
    std::string state;
    std::string belong;
    __Array *ary_img;
    ///////////////////
    void reset()
    {
        taskId = -1;
        imgNumber = 0;
        title = "";
        url = "";
        belong = "";
        state = TASK_STATE_wait;
        ary_img = nullptr;
    }

    __Dictionary *getDic()
    {
        // 取回 对应的Dictionary，设置信息，并保存。
        __Dictionary *oneInfo = __Dictionary::create();
        oneInfo->setObject(__String::createWithFormat("%s", title.c_str() ),  INFO_ONE_TITLE);
        oneInfo->setObject(__String::createWithFormat("%s", belong.c_str() ),  INFO_ONE_BELONG);
        oneInfo->setObject(__String::createWithFormat("%d", imgNumber ),  INFO_ONE_IMG_NUMBER );
        oneInfo->setObject(__String::createWithFormat("%s", state.c_str() ), INFO_ONE_STATE);
        oneInfo->setObject(__String::createWithFormat("%s", url.c_str() ), INFO_ONE_WebURL);
        oneInfo->setObject(__String::createWithFormat("%d", taskId ), INFO_ONE_ID );
        oneInfo->setObject( ary_img, INFO_ONE_IMG_ARY);
        return oneInfo;
    }
    static OneTaskInfo getFromDic(__Dictionary *oneInfo)
    {
        OneTaskInfo one;
        one.reset();
        one.taskId = ((__String *)oneInfo->objectForKey(INFO_ONE_ID))->intValue();
        one.imgNumber = ((__String *)oneInfo->objectForKey(INFO_ONE_IMG_NUMBER))->intValue();
        one.title = ((__String *)oneInfo->objectForKey(INFO_ONE_TITLE))->getCString();
        one.url = ((__String *)oneInfo->objectForKey(INFO_ONE_WebURL))->getCString();
        one.belong = ((__String *)oneInfo->objectForKey(INFO_ONE_BELONG))->getCString();
        one.state = ((__String *)oneInfo->objectForKey(INFO_ONE_STATE))->getCString();
        one.ary_img = __Array::create();
        __Array *ary = (__Array *)oneInfo->objectForKey(INFO_ONE_IMG_ARY);
        for( int i=0;i<ary->count();i++ ){
            __String *oneImg = (__String *)ary->getObjectAtIndex(i);
            one.ary_img->addObject( __String::create(oneImg->getCString()));
        }
        return one;
    }
};



// 单个图片下载任务信息。
struct OneImageDownInfo
{
    int     taskId;         // 所属任务 ID
    int     inTaskIdx;      // 任务内顺序
    bool    isBusying;     // 完成的标记。
    std::string imgURL;     // 图片url
    // ---------------------------------------
    void reset()
    {
        taskId = -1;
        inTaskIdx = -1;
        isBusying = false;
        imgURL = "";
    }
    bool canUse(){ return taskId==-1; }
};




#pragma -mark
class HttpNode: public Node
{
public:
    CREATE_FUNC(HttpNode);
    bool init();
    void setIsSingle(bool is);

public:
    void downWeb(std::string url, int taskId);
    void downImage(std::string imgUrl, OneImageDownInfo *imgInfo);
    bool isBusying();
    bool isImgExist(int taskId, int inTaskIdx);
    void setSingleDownEndFunc(std::function<void(std::string url)> f);

private:
    void onConnectToWebEnd(HttpNode *sender ,cocos2d::network::HttpResponse *response);
    void onDownLoadImageEnd(HttpNode *sender ,cocos2d::network::HttpResponse *response);

private:
    std::function<void(std::string url)> m_singleDownEndFunc;
    bool m_isBusying;
    bool m_isSingle;
    std::string m_myURL;
    int m_taskId;
    int m_inTaskIdx;
    OneImageDownInfo downImgInfo;
};







#endif






